﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SNGenerator;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Windows.Threading;

namespace LicenceKeyGenerator
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window,INotifyPropertyChanged
    {
        private BackgroundWorker _bg = new BackgroundWorker();
        public MainWindow()
        {
           
            this.DataContext = this;
            IsBusy = false;
            InitializeComponent();
            _bg.DoWork += new DoWorkEventHandler(_bg_DoWork);
            _bg.RunWorkerCompleted += new RunWorkerCompletedEventHandler(_bg_RunWorkerCompleted);
            _bg.WorkerReportsProgress = true;
            _bg.ProgressChanged += new ProgressChangedEventHandler(_bg_ProgressChanged);
            //Keys.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Keys_CollectionChanged);
        }

        void Keys_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            Dispatcher.BeginInvoke(new Action(() =>
            {
                if (e.Action == NotifyCollectionChangedAction.Add)
                {
                    RandomProgress = (int)(Keys.Count / Convert.ToDouble(txtNumber.Text) * 100);
                }
            }), DispatcherPriority.Background);
           
        }

        void _bg_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            RandomProgress = e.ProgressPercentage;
        }

        

        void _bg_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            Dispatcher.BeginInvoke(new Action(() =>
            {
                if (_currentIndex < Convert.ToInt32(txtNumber.Text))
                {
                    _bg.RunWorkerAsync();
                }
                else
                {
                    _currentIndex = 0;
                    IsBusy = false;
                }
            }),DispatcherPriority.Background);
            
        }
        private int _currentIndex = 0;
        void _bg_DoWork(object sender, DoWorkEventArgs e)
        {
            this.Dispatcher.BeginInvoke(new Action(() =>
            {
                if (_currentIndex == 0)
                {
                    IsBusy = true;
                    Keys.Clear();
                }
                
                var licenseLength = (LicenseLength)Enum.Parse(typeof(LicenseLength), "SN" + cmbNumber.Text);

               
                    string serialNumber = LicenseGenerator.GetSerialKeyAlphaNumaric(licenseLength);
                    if (!Keys.Contains(serialNumber))
                    {
                       
                        Keys.Add(serialNumber);
                        RandomProgress = (int)(Keys.Count / Convert.ToDouble(txtNumber.Text) * 100);
                        _currentIndex++;
                    }
                    else
                    {
                        _currentIndex--;
                    }
                
            }
                ), DispatcherPriority.Background);
        }

        private void RandomStart(object sender, RoutedEventArgs e)
        {
            _bg.RunWorkerAsync();

        }

        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged!=null)
            PropertyChanged.Invoke(this,new PropertyChangedEventArgs(propertyName));
        }
        private ObservableCollection<string> _keys = new ObservableCollection<string>();

        public ObservableCollection<string> Keys
        {
            get { return _keys; }
            set { _keys = value;
            NotifyPropertyChanged("Keys");
            }
        }
      
        int _randomProgress;
        public int RandomProgress
        {
            get
            {
                return _randomProgress;
            }
            set
            {
                _randomProgress = value;
                NotifyPropertyChanged("RandomProgress");
            }
        }

        private bool _isBusy;

        public bool IsBusy
        {
            get { return _isBusy; }
            set
            {
                _isBusy = value;
                NotifyPropertyChanged("IsBusy");
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        private void RandomCopyToClipboard(object sender, RoutedEventArgs e)
        {
            var result = "";
            foreach (var key in Keys)
            {
                result = result + key + Environment.NewLine;
            }
            Clipboard.SetText(result);
        }
    }
}
